package com.longcheer.weibo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.longcheer.weibo.R;
import com.longcheer.weibo.util.Utils;
import com.longcheer.weibo.vo.Status;
import com.longcheer.weibo.vo.User;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeiboListAdapter extends BaseAdapter {
    
    private static final String TAG = "WeiboListAdapter";
    
    private static SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
    
    private Context mContext;
    private List<Status> mList; 
    private ImageManager mImageManager;
    
    public int scrollRefreshState;
    
    private final LayoutInflater mInflater;
    
    public static final int VIEW_TYPE_WEIBO = 0;
    //public static final int VIEW_TYPE_REFRESH = 1;
    public static final int VIEW_TYPE_MORE = 1;
    
    public WeiboListAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mImageManager = ImageManager.getInstance();
        mList = new ArrayList<Status>();
    }
    
    public void setStatusList(List<Status> statuses) {
        mList = statuses;
    }
    
    public List<Status> getStatusList() {
        return mList;
    }
    
    public void addStatusList(List<Status> statuses) {
        mList.addAll(statuses);
    }

    @Override
    public int getCount() {
        return mList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
//        if (position == 0) {
//            return "";
//        } else if (position == getCount()-1) {
//            return "";
//        } else {
//            return mList.get(position-1);    
//        }
        if (position == getCount()-1) {
            return "";
        } else {
            return mList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position+1;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == getCount()-1) {
            return VIEW_TYPE_MORE;
        } else {
            return VIEW_TYPE_WEIBO;    
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        if (type == VIEW_TYPE_MORE) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.more, null);
            }
            return convertView;
        } 
        
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.weibo_list_item, null);
            holder = new ViewHolder();
            holder.userAvatar = (ImageView)convertView.findViewById(R.id.iv_user_avatar);
            holder.userAvatarRound = (ImageView)convertView.findViewById(R.id.iv_user_avatar_round);
            holder.userNick = (TextView)convertView.findViewById(R.id.tv_user_nick);
            holder.weiboTime = (TextView)convertView.findViewById(R.id.tv_weibo_time);
            
            holder.weiboText = (TextView)convertView.findViewById(R.id.tv_weibo_text);
            holder.weiboImage = (ImageView)convertView.findViewById(R.id.iv_weibo_image);
            
            holder.repostBody = (LinearLayout)convertView.findViewById(R.id.ll_weibo_repost_body);
            holder.repostText = (TextView)convertView.findViewById(R.id.tv_weibo_repost_text);
            holder.repostImage = (ImageView)convertView.findViewById(R.id.iv_weibo_repost_image);
            
            holder.weiboSource = (TextView)convertView.findViewById(R.id.tv_weibo_source);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag(); 
        }

        Status status = (Status)getItem(position);      
        final User user = status.getUser();
        
        holder.userAvatar.setImageResource(R.drawable.ic_launcher);
        holder.userAvatarRound.setImageResource(R.drawable.portrait_normal);
        mImageManager.loadImage(user.getProfileImageUrl(), new ImageManager.Callback() {
            @Override
            public void imageLoaded(Bitmap bitmap, String imageUrl) {
                holder.userAvatar.setImageBitmap(bitmap);
                if (user.isVerified()) {
                    holder.userAvatarRound.setImageResource(R.drawable.portrait_v);
                }
            }
        });
        
        
        holder.userNick.setText(user.getScreenName());
        String weiboTime = sdf.format(status.getCreatedAt());
        holder.weiboTime.setText(weiboTime);
        holder.weiboText.setText(status.getText());

        if (!Utils.isEmpty(status.getThumbnailPic())) {
            holder.weiboImage.setVisibility(View.VISIBLE);
            holder.weiboImage.setImageResource(R.drawable.preview_pic_loading);
            mImageManager.loadImage(status.getThumbnailPic(), new ImageManager.Callback() {
                @Override
                public void imageLoaded(Bitmap bitmap, String imageUrl) {
                    holder.weiboImage.setImageBitmap(bitmap);
                }
            });
        } else {
            holder.weiboImage.setVisibility(View.GONE);
        }

        Status retweet = status.getRetweetedStatus();
        if (retweet != null) {
            holder.repostBody.setVisibility(View.VISIBLE);
            String retweetText = String.format("%s: %s", retweet.getUser().getScreenName(), retweet.getText());
            holder.repostText.setText(retweetText);
            if (!Utils.isEmpty(retweet.getThumbnailPic())) {
                holder.repostImage.setVisibility(View.VISIBLE);
                holder.repostImage.setImageResource(R.drawable.preview_pic_loading);
                mImageManager.loadImage(retweet.getThumbnailPic(), new ImageManager.Callback() {
                    @Override
                    public void imageLoaded(Bitmap bitmap, String imageUrl) {
                        holder.repostImage.setImageBitmap(bitmap);
                    }
                });
            } else {
                holder.repostImage.setVisibility(View.GONE);
            }
        } else {
            holder.repostBody.setVisibility(View.GONE);
        }
        
        holder.weiboSource.setText("来自:" + status.getSource().getShow());
//        String repostComment = String.format("转发:%d|评论:%d", 
//                status.getRepostsCount(), status.getCommentsCount());
//        holder.weiboRepostComment.setText(repostComment);
        return convertView;
        
    }
    

    public static class ViewHolder {
        public ImageView userAvatar;
        public ImageView userAvatarRound;
        
        public TextView userNick;
        public TextView weiboTime;
        
        public TextView weiboText;
        public ImageView weiboImage;
        
        public LinearLayout repostBody;
        public TextView repostText;
        public ImageView repostImage;
        
        public TextView weiboSource;
    }
}
